$(document).ready(function()
    {
        $.table_master = null;

        $.init_master = function()
        {
            $.ajax({
                url: base_url + 'br-02/order_barang/get_non_realisasi',
                type: 'post',
                success : function(data)
                {
                    $.table_master = $('#order_master').dataTable({
                        "bJQueryUI": true,
                        "sPaginationType": "full_numbers",
                        "aaData": data.data,
                        "aoColumns":[
                        {
                            'sTitle' : 'ID Order',
                            'mDataProp' : 'ID_ORDER'
                        },
                        {
                            'sTitle' : 'ID Suplier',
                            'mDataProp' : 'ID_SUPLIER'
                        },
                        {
                            'sTitle' : 'ID Toko',
                            'mDataProp' : 'ID_TOKO'
                        },
                        {
                            'sTitle' : 'ID Faktur',
                            'mDataProp' : 'ID_FAKTUR'
                        },
                        {
                            'sTitle' : 'Nomer Order',
                            'mDataProp' : 'NOMER_ORDER'
                        },
                        {
                            'sTitle' : 'Tanggal Order',
                            'mDataProp' : 'TGL_ORDER'
                        },
                        {
                            'sTitle' : 'Diskon',
                            'mDataProp' : 'DISKON'
                        },
                        {
                            'sTitle' : 'Total',
                            'mDataProp' : 'TOTAL'
                        },
                        ]
                    });
                },
                error: function(res)
                {
                    alert(res.responseText);
                },
                dataType: 'json'
            });
        }

        $.init_master_faktur = function()
        {
            $.ajax({
                url: base_url + 'br-02/faktur/get_all',
                type: 'post',
                success : function(data)
                {
                    $.table_master = $('#faktur_master').dataTable({
                        "bJQueryUI": true,
                        "sPaginationType": "full_numbers",
                        "aaData": data.data,
                        "aoColumns":[
                        {
                            'sTitle' : 'ID Faktur',
                            'mDataProp' : 'ID_FAKTUR'
                        },
                        {
                            'sTitle' : 'ID Suplier',
                            'mDataProp' : 'ID_SUPLIER'
                        },
                        {
                            'sTitle' : 'Nomer Faktur',
                            'mDataProp' : 'NOMER_FAKTUR'
                        },
                        {
                            'sTitle' : 'Tanggal Faktur',
                            'mDataProp' : 'TGL_FAKTUR'
                        },
                        {
                            'sTitle' : 'Total',
                            'mDataProp' : 'TOTAL'
                        },
                        ]
                    });
                },
                error: function(res)
                {
                    alert(res.responseText);
                },
                dataType: 'json'
            });
        }

        $.init_master_trx = function()
        {
            $.ajax({
                url: base_url + 'br-06/pembelian/get_bukti/',
                type: 'post',
                success : function(data)
                {
                    $.table_master = $('#trx_master').dataTable({
                        "bJQueryUI": true,
                        "sPaginationType": "full_numbers",
                        "aaData": data.data,
                        "aoColumns":[
                        {
                            'sTitle' : 'ID Bukti Transaksi ',
                            'mDataProp' : 'ID_BUKTI_TRANSAKSI'
                        },
                        {
                            'sTitle' : 'Nomer Transaksi',
                            'mDataProp' : 'NOMER_TRANSAKSI'
                        },
                        {
                            'sTitle' : 'Jenis Data',
                            'mDataProp' : 'NAMA_JENIS_DATA'
                        },
                        {
                            'sTitle' : 'Jenis Transaksi',
                            'mDataProp' : 'NAMA_JENIS_TRANSAKSI'
                        },
                        {
                            'sTitle' : 'Tanggal Transaksi',
                            'mDataProp' : 'TGL_TRANSAKSI'
                        },
                        {
                            'sTitle' : 'Keterangan',
                            'mDataProp' : 'KETERANGAN'
                        },
                        ]
                    });
                },
                error: function(res)
                {
                    alert(res.responseText);
                },
                dataType: 'json'
            });
        }

        $('#dialog-order').dialog({
            title: 'Data order',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $('#dialog-faktur').dialog({
            title: 'Data faktur',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $('#dialog-trx').dialog({
            title: 'Data bukti',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });
        
        $.resultorder = function(data)
        {
            if($('td:first', data).html() != 'No matching records found')
            {
                 $("#ID_ORDER").val($('td:first', data).html());
                $("#NO_ORDER").val($('td:eq(4)', data).html());
            }
        }

        $.resultfaktur = function(data)
        {
            if($('td:first', data).html() != 'No matching records found')
            {
                $("#ID_FAKTUR").val($('td:first', data).html());
                $("#NO_FAKTUR").val($('td:eq(2)', data).html());
            }
        }

        $.resultbukti = function(data)
        {
            if($('td:first', data).html() != 'No matching records found')
            {
                $("#ID_BUKTI").val($('td:first', data).html());
                $("#NO_BUKTI").val($('td:eq(1)', data).html());
            }
        }

        $('#order_master tbody tr').live('click', function()
        {
            $.datatableorderRowClicked(this);
        });

        $.datatableorderRowClicked = function(that)
        {
            $.resultorder(that);
            $('#dialog-order').dialog('close');
        }

        $('#faktur_master tbody tr').live('click', function()
        {
            $.datatablefakturRowClicked(this);
        });

        $.datatablefakturRowClicked = function(that)
        {
            $.resultfaktur(that);
            $('#dialog-faktur').dialog('close');
        }

        $('#trx_master tbody tr').live('click', function()
        {
            $.datatabletrxRowClicked(this);
        });

        $.datatabletrxRowClicked = function(that)
        {
            $.resultbukti(that);
            $('#dialog-trx').dialog('close');
        }

        $("#NO_ORDER").click(function()
        {
            $('#dialog-order').dialog('open');
        });

        $("#NO_FAKTUR").click(function(){
            $('#dialog-faktur').dialog('open');
        });

        $("#NO_BUKTI").click(function(){
            $('#dialog-trx').dialog('open');
        });

        $("#realisasi-pembelian").submit(function() {
            return false;
        });

        $.init_master();
        $.init_master_faktur();
        $.init_master_trx();

        $.notif_order = function(data)
        {
            $("#barang_order_master tbody").html("");
            if(data.data != null)
            {
                var total = 0;
                for(var i = 0; i < data.data.length; i++)
                {
                    var newData = data.data[i];
                    total += newData.TOTAL_HARGA * 1;
                    $("#barang_order_master tbody").append('<tr>\n\
                        <td>'+newData.ID_BARANG+'</td>\n\
                        <td>'+newData.NAMA_BARANG+'</td>\n\
                        <td>'+newData.JUMLAH+'</td>\n\
                        <td>'+newData.TOTAL_HARGA / newData.JUMLAH+'</td>\n\
                        <td>'+newData.TOTAL_HARGA+'</td>\n\
                    </tr>');
                }
                $("#barang_order_master tfoot").html('<tr>\n\
                        <th colspan="4">Total</th>\n\
                        <th>'+total+'</th>\n\
                        <th></th>\n\
                    </tr>');
                $("#TOTAL").val(total);
            }
        }

        $.notif_faktur = function(data)
        {
            $("#barang_faktur_master tbody").html("");
            if(data.data != null)
            {
                var total = 0;
                for(var i = 0; i < data.data.length; i++)
                {
                    var newData = data.data[i];
                    total += newData.TOTAL_HARGA * 1;
                    $("#barang_faktur_master tbody").append('<tr>\n\
                        <td>'+newData.ID_BARANG+'</td>\n\
                        <td>'+newData.NAMA_BARANG+'</td>\n\
                        <td>'+newData.JUMLAH+'</td>\n\
                        <td>'+newData.TOTAL_HARGA / newData.JUMLAH+'</td>\n\
                        <td>'+newData.TOTAL_HARGA+'</td>\n\
                    </tr>');
                }
                $("#barang_faktur_master tfoot").html('<tr>\n\
                        <th colspan="4">Total</th>\n\
                        <th>'+total+'</th>\n\
                        <th></th>\n\
                    </tr>');
                $("#TOTAL").val(total);
            }
        }

        $.refresh_order_barang = function()
        {
            $("#barang_order_master").show();
            $.data_ajax('br-02/detail_order/get_all/' + $("#ID_ORDER").val(), null, $.notif_order);
        }

        $.refresh_faktur_barang = function()
        {
            $("#barang_faktur_master").show();
            $.data_ajax('br-02/detail_faktur/get_all/' + $("#ID_FAKTUR").val(), null, $.notif_faktur);
        }

        $("#cek_order").click($.refresh_order_barang);
        $("#cek_faktur").click($.refresh_faktur_barang);
        
        $("#barang_order_master").hide();
        $("#barang_faktur_master").hide();

        $("#realisasi").click(function(){
            var sure = confirm("Anda yakin untuk realisasi?");
            if (sure){
                $.call_ajax('br-06/pembelian/realisasi_po/', $("#realisasi-pembelian"), $.notif_realisasi);
            }
        });

        $.notif_realisasi = function(data){
            if (data.data) alert("Rencana pembelelian berhasil di realisasikan.");
            else alert("Rencana pembelelian gagal di realisasikan.");
        }

    }
    );